import com.yourdomain.spreadshop.gui.GUI;

/**
 * The ScrollBar class simply provides the scrollbar movie clips
 * with some functions to manage the user input and to scroll the
 * container movie clip.
 */
class com.yourdomain.spreadshop.gui.mod.lib.ScrollBar {
	// The movie clips.
	public var clip:MovieClip;	public var context:MovieClip;	// The reference to the GUI.	private var _gui:GUI;

	private static var _SNAP_TO_LIMITS:Number = 1;
	/**
	 * ScrollBar Constructor
	 */
	public function ScrollBar(
		gui:GUI,		scrollbar:MovieClip,
		container:MovieClip,
		visibility:Boolean	) {		_gui = gui;
		clip = scrollbar;		context = container;
		// Setup main settings.
		clip.con = context;
		clip.YMIN = 0;
		clip.YMAX = clip.track._height - clip.slider._height;
		clip.SNAP = _SNAP_TO_LIMITS;
		// Show or hide scrollbar.		clip._visible = visibility;
		// Initialize the scrollbar.
		setup();
	}

	/**
	 * Setup ScrollBar behavior.
	 */
	private function setup():Void {
		// Get settings.
		var scr_h:Number = clip._height - 4;
		var height:Number = (clip.con._height - scr_h);
		clip.MULTIPLIER = height / (clip.YMAX - clip.YMIN) * -1;
		// Reset the position of the movie clips.
		clip.con._y = 0;
		clip.slider._y = clip.YMIN;
		// If scrollbar is visible, setup behavior.
		if(clip._visible) {
			clip.border._visible = false;			clip.gui = _gui;			clip.startDragging = function():Void {
				this.slider.startDrag(false, 0, this.YMIN, 0, this.YMAX);
				this.border._visible = true;
				this.onEnterFrame = this.onDragging;
				this.gui.startScrolling();			};
			clip.stopDragging = function():Void {
				this.slider.stopDrag();
				this.border._visible = false;
				delete this.onEnterFrame;
				this.gui.stopScrolling();			};
			clip.onDragging = function():Void {
				// Snap to limits.
				if(this.slider._y < this.YMIN + this.SNAP) {
					this.slider._y = this.YMIN;
				} else if(this.slider._y > this.YMAX - this.SNAP) {
					this.slider._y = this.YMAX;
				}
				this.con._y = (this.slider._y - this.YMIN)
					* this.MULTIPLIER;
				this.con._y >>= 0;
			};
			clip.slider.bt.useHandCursor = false;
			clip.slider.bt.tabEnabled = false;
			clip.slider.bt.onPress = function():Void {
				this._parent._parent.startDragging();
			};
			clip.slider.bt.onReleaseOutside =
			clip.slider.bt.onRelease = function():Void {
				this._parent._parent.stopDragging();
			};
			clip.track.useHandCursor = false;
			clip.track.tabEnabled = false;
			clip.track.onPress = function():Void {
				var slider:MovieClip = this._parent.slider;
				slider._y = (this._ymouse + slider._width * .2) >> 0;
				this._parent.onDragging();
			};
			clip.onMouseWheel = function(delta:Number):Void {
				if(this._parent.isOver()) {
					var dxs:Number = (this.YMAX - this.YMIN);					var dxf:Number = dxs / (this.con._height - dxs) * 10;
					this.slider._y -= delta * dxf;					this.onDragging();
				}
			};
    		Mouse.addListener(clip);
		} else {
			Mouse.removeListener(clip);
		}
	}

}